package com.migration.model.migration.scripts;

import com.migration.model.migration.AbstractMigrationScript;

/**
 * 风险区域表(risk_area)迁移脚本
 * 使用跨数据库迁移功能从旧库查询数据，插入到新库
 */
public class A001_RiskAreaMigrationScript extends AbstractMigrationScript {

    /**
     * 构造函数，初始化迁移配置
     */
    public A001_RiskAreaMigrationScript() {
        super(
            "risk_area",
            "风险区域数据迁移",
            "ra_area",
            "risk_area",
            1
        );
    }

    /**
     * 生成迁移数据的SQL
     * 此处的SQL将在跨库迁移中使用，从旧库查询数据插入新库
     *
     * @return 迁移数据的SQL
     */
    @Override
    protected String generateMigrationSql() {
        // 注意：这里的SQL仍然是从"旧"数据库查询，插入到"新"数据库
        // 但在具体执行时，我们会通过跨库迁移服务分别连接不同的数据库
        return "INSERT INTO risk_area (\n" +
                "  id,\n" +
                "  tenant_id,\n" +
                "  department_id,\n" +
                "  area_name,\n" +
                "  manager_id,\n" +
                "  zone_range,\n" +
                "  zone_type,\n" +
                "  function_desc,\n" +
                "  response_type,\n" +
                "  create_time,\n" +
                "  update_time,\n" +
                "  create_by,\n" +
                "  update_by,\n" +
                "  is_del\n" +
                ")\n" +
                "SELECT\n" +
                "  id,\n" +
                "  1 as tenant_id,\n" +
                "  tenant_id AS department_id,\n" +
                "  LEFT(area_name, 50),\n" +
                "  area_principal,\n" +
                "  LEFT(area_scope, 255),\n" +
                "  area_type,\n" +
                "  LEFT(function_desc, 255),\n" +
                "  NULLIF(reaction_type, '') + 0,\n" +
                "  DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s'),\n" +
                "  DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%s'),\n" +
                "  1,\n" +
                "  1,\n" +
                "  is_del\n" +
                "FROM risk_lunnan_test.ra_area\n" +
                "WHERE is_del = 0;";
    }

    /**
     * 获取源数据SQL
     * 用于从旧数据库查询数据
     *
     * @return 源数据查询SQL
     */
    public String getSourceSql() {
        return "SELECT\n" +
                "  id,\n" +
                "  tenant_id,\n" +
                "  area_name,\n" +
                "  area_principal,\n" +
                "  area_scope,\n" +
                "  area_type,\n" +
                "  function_desc,\n" +
                "  reaction_type,\n" +
                "  create_time,\n" +
                "  update_time,\n" +
                "  is_del\n" +
                "FROM risk_lunnan_test.ra_area\n" +
                "WHERE is_del = 0;";
    }

    /**
     * 获取目标数据SQL模板
     * 用于将处理后的数据插入新数据库
     *
     * @return 目标数据SQL模板
     */
    public String getTargetSqlTemplate() {
        return "INSERT INTO risk_area (\n" +
                "  id,\n" +
                "  tenant_id,\n" +
                "  department_id,\n" +
                "  area_name,\n" +
                "  manager_id,\n" +
                "  zone_range,\n" +
                "  zone_type,\n" +
                "  function_desc,\n" +
                "  response_type,\n" +
                "  create_time,\n" +
                "  update_time,\n" +
                "  create_by,\n" +
                "  update_by,\n" +
                "  is_del\n" +
                ")";
    }
}
